var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/ip', async function(req, res, next) {
  await by_db('select * from by_ip order by id desc limit 1', []).then(data => {
    res.send(data[0] || {})
  }).catch(e => {
    res.send(e)
  })
});

router.post('/updateIp', async function(req, res, next) {
  const ip = req.body.ip || ''
  if (ip) {
    const time = new Date().getTime()
    await by_db('insert into by_ip (ip, time) values (?,?)', [ip, time]).then(data => {
      res.send({
        code: 0,
        data: '更新成功'
      })
    }).catch(e => {
      res.send({
        code: -1,
        data: e
      })
    })
  } else {
    res.send({
      code: -1,
      data: '没有ip'
    })
  }

});

module.exports = router;
